<?php
if(!isset($_REQUEST['flag']))
{
$send=0;
$custid=111;
}
else if($_REQUEST['flag']==1)
{

$send=1;

}
?>
<?php

$message='


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
    <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
    <TITLE></TITLE>
	<STYLE TYPE="text/css">
		@page { margin: 1in }
		P { margin-bottom: 0.08in; direction: ltr; line-height: 115%; widows: 2; orphans: 2 }
		P.western { so-language: en-US }
		.bt{ background:#666666; width:100px; padding:4px; height:28px; color:#FFFFFF; font-size:12px;}
	</STYLE>
</HEAD>
<BODY LANG="en-IN" DIR="LTR">
<TABLE WIDTH=602 CELLPADDING=1 CELLSPACING=0 BGCOLOR="#ffffff" frame="below">
    
    <TR>
      <TD BGCOLOR="#ffffff" STYLE="border: none; margin-bottom: 0in ; padding: 0in">
        <img  src="http://firstnorthglobal.com/uat/shreelogo.png" alt="name" width="55" height="56.5" />
      
     </TD>      

     <TD WIDTH=701 BGCOLOR="#ffffff" STYLE="border: none; padding: 0in">
            
    	<P LANG="en-US" CLASS="western"  ALIGN=center STYLE="margin-bottom: 0in; background: #ffffff; line-height: 100%">
		<FONT COLOR="#0966ba"><FONT FACE="Arial, serif"><FONT SIZE=5><U><B>'.$_REQUEST['clientname'].'</B></U></FONT></FONT></FONT></P>
		<P LANG="en-US"  CLASS="western" ALIGN=center STYLE="margin-bottom: 0in; background: #ffffff; line-height: 100%"><br>
		<FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=4 STYLE="font-size: 11pt"><B>Ledger From Date:</B></FONT></FONT></FONT><FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=2 STYLE="font-size: 10pt"><B>
    		'.$_REQUEST['startdate'].'</B></FONT></FONT></FONT></P><br>
        <P LANG="en-US" CLASS="western" ALIGN=LEFT STYLE="margin-bottom: 0in; line-height: 100%">
        </P>
       </TD>
    </TR>
</table>
    <TABLE WIDTH=602 CELLPADDING=1 CELLSPACING=0 BGCOLOR="#ffffff">
    
    <COL WIDTH=401>
	<COL WIDTH=297>
           
	<TR>
       	<TD WIDTH=401 BGCOLOR="#ffffff" STYLE="border: none; padding: 0in">
     		<P LANG="en-US" CLASS="western"><FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=2 STYLE="font-size: 9pt"><B>Member
			Name</B></FONT></FONT></FONT><FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=2 STYLE="font-size: 9pt">:
			'.$_REQUEST['custdesc'].'</FONT></FONT></FONT></P>
		</TD>
		<TD WIDTH=297 BGCOLOR="#ffffff" STYLE="border: none; padding: 0in">
			<P LANG="en-US" CLASS="western" ALIGN=CENTER><FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=2 STYLE="font-size: 9pt"><B>As on Date</B></FONT></FONT></FONT><FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=2 STYLE="font-size: 9pt">:
			'.$_REQUEST['date'].'</FONT></FONT></FONT></P>
		</TD>
	</TR>
	<TR>
		<TD WIDTH=401 BGCOLOR="#ffffff" STYLE="border: none; padding: 0in">
			<P LANG="en-US" CLASS="western"><FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=2 STYLE="font-size: 9pt"><B>Membership
			Number</B></FONT></FONT></FONT><FONT COLOR="#222222"><FONT FACE="Arial, serif"><FONT SIZE=2 STYLE="font-size: 9pt">:
			'.$_REQUEST['custid'].'</FONT></FONT></FONT></P>
		</TD>
		
		<TD WIDTH=401 BGCOLOR="#ffffff" STYLE="border: none; padding: 0in">&nbsp;
		
		</TD>
		
	</TR>
</TABLE>


';

//echo $message;
?>


<?php

 class phpReportGenerator
{
	var $mysql_resource;
	var $header;
	var $foolter;
    var $fields = array();
	var $cellpad;
	var $cellspace;
	var $border;
	var $width;
	var $modified_width;
	var $header_color;
	var $header_textcolor;
	var $header_alignment;
	var $body_color;
	var $body_textcolor;
	var $body_alignment;
	var $surrounded;
	
	function generateReport($message,$send)
	{
	
	$val=0;
	$emailbody=array();
	$emailbody[$val]=$message;
	$val=$val+1;
	
		$this->border = (empty($this->border))?"0":$this->border;
		$this->cellpad = (empty($this->cellpad))?"1":$this->cellpad;
		$this->cellspace = (empty($this->cellspace))?"0":$this->cellspace;
		$this->width = (empty($this->width))?"100%":$this->width;
		$this->header_color = (empty($this->header_color))?"#FFFFFF":$this->header_color;
		$this->header_textcolor = (empty($this->header_textcolor))?"#000000":$this->header_textcolor;		
		$this->header_alignment = (empty($this->header_alignment))?"Center":$this->header_alignment;
		$this->body_color = (empty($this->body_color))?"#FFFFFF":$this->body_color;
		$this->body_textcolor = (empty($this->body_textcolor))?"#000000":$this->body_textcolor;
		$this->body_alignment = (empty($this->body_alignment))?"Right":$this->body_alignment;
		$this->surrounded = (empty($this->surrounded))?false:true;
		$this->modified_width = ($this->surrounded==true)?"100%":$this->width;
		
		//echo "modified_width : ".$this->modified_width."<br>"; 
		
		if (!is_resource($this->mysql_resource))
			die ("User doesn't supply any valid mysql resource after executing query result");

		/*
		* Lets calculate how many fields are there in supplied resource
		* and store their name in $this->fields[] array
		*/
		
		$field_count = mysql_num_fields($this->mysql_resource);
		$i = 0; 
		
		while ($i < $field_count)
		{
			$field = mysql_fetch_field($this->mysql_resource);
			$this->fields[$i] = $field->name;
			$this->fields[$i][0] = strtoupper($this->fields[$i][0]);
			$i++;
		}
				
		/*
		* Now start table generation
		* We must draw this table according to number of fields
		*/
		$emailbody[$val]= "<b><i>".$this->header."</i></b>";
		$val=$val+1;
	
		//echo "<P></P>";
		
		//Check If our table has to be surrounded by an additional table
		//which increase style of this table
		if ($this->surrounded == true) 
			//echo "<table width='$this->width'  border='1' cellspacing='0' cellpadding='0'><tr><td>";
			$emailbody[$val]= "<TABLE WIDTH=602 CELLPADDING=3 CELLSPACING=0 BGCOLOR='#ffffff'>
            <COL WIDTH=92>
	        <COL WIDTH=1922>
	        <COL WIDTH=58>
	        <COL WIDTH=60>
	        <COL WIDTH=60>";
			$val=$val+1;
		//echo "<table width='$this->modified_width'  border='$this->border' cellspacing='$this->cellspace' cellpadding='$this->cellpad'>";
		$emailbody[$val]= "<tr bgcolor = '#d4d4d4'>";
		
		//Header Draw
		$val=$val+1;
		$start1=$val;
		for ($i = 0; $i< $field_count; $i++)
		{
		if($i<2) continue; //skip printing 1st and 2nd col
			//Now Draw Headers
			//echo "<th align = '$this->header_alignment'><font color = '$this->header_textcolor'>&nbsp;".$this->fields[$i]."</font></th>";
			$emailbody[$val]=  "<td align = '$this->header_alignment' ; WIDTH=72 STYLE='border-top: none; border-bottom: 1px dotted #cccccc; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.03in; padding-left: 0in; padding-right: 0in>
             <P LANG=en-US CLASS=western ALIGN=CENTER><SPAN STYLE=text-transform: uppercase><FONT COLOR='#333333'><FONT FACE='Arial, serif'><FONT SIZE=1 STYLE='font-size: 8pt'><B>".$this->fields[$i]."</B></FONT></FONT></FONT></SPAN></P></td>";
			 $val=$val+1; 
		}
		$end1=$val;
		$val=$end1;
    	$emailbody[$val]=  "</tr>";
		$val=$val+1;
		
		$start2=$val;
		//Now fill the table with data
		while ($rows = mysql_fetch_row($this->mysql_resource))
		{
		     $o = strpos($rows[3],'otal Outstanding');
			 if($o > 0)
             {
		    	$a= "<tr align = '$this->body_alignment' bgcolor = '#d4d4d4'>";			
			 }
			 else
			 {
			    $a= "<tr align='$this->body_alignment' bgcolor = '#ffffff'>";
			 }
			 $emailbody[$val]=$a;
			
			
			$val=$val+1;
			
			for ($i = 0; $i < $field_count; $i++)
			{
			
			if($i<2) continue; //skip printing 1st and 2nd col
				$len = strlen($rows[$i]);
				if($len =1) 
				  $desc = $rows[$i];
				else
				  $desc = '';
				
                $len1 = strlen($rows[$i]);
    			if($len1 !=1) 
				  $desc1  = $rows[$i];
				else
				  $desc1 = '';
         
            if($o > 0) //(is_numeric($o))
                {
				$b= "<TD align = center WIDTH=322 STYLE='border-top: none; border-bottom: 1px solid #cccccc; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.03in; padding-left: 0in; padding-right: 0in'>
				<P LANG='en-US' CLASS='western'><FONT FACE='Arial, serif' SIZE=1 STYLE='font-size: 10pt'><B>".$desc1."</B></FONT></P></TD>";
				}
				else
				{
				/*
					// old 
					printf("<TD align = center WIDTH=322 STYLE='border-top: none; border-bottom: 1px solid #cccccc; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.03in; padding-left: 0in; padding-right: 0in'>
					<P LANG='en-US' CLASS='western' %s><FONT FACE='Arial, serif' SIZE=1>%s</FONT></P></TD>",($i==3) ? 'align=left': '',$desc);
					//old
					*/
					if($i==3)
					$b="<TD align = center WIDTH=322 STYLE='border-top: none; border-bottom: 1px solid #cccccc; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.03in; padding-left: 0in; padding-right: 0in'>
				<P LANG='en-US' CLASS='western' align='left'><FONT FACE='Arial, serif' SIZE=1 >$desc</FONT></P></TD>";
					
					else
					$b="<TD align = center WIDTH=322 STYLE='border-top: none; border-bottom: 1px solid #cccccc; border-left: none; border-right: none; padding-top: 0in; padding-bottom: 0.03in; padding-left: 0in; padding-right: 0in'>
				<P LANG='en-US' CLASS='western' ><FONT FACE='Arial, serif' SIZE=1 >$desc</FONT></P></TD>";
					
					
				}
				$emailbody[$val]=$b;
				$val=$val+1;
				
			} // for end
			$val=$val+1;
				$d= "</tr>";
			//echo $d;
			$emailbody[$val]=$d;
			$val=$val+1;
		}
		$Te= "</table>";
		//echo $Te;
			$emailbody[$val]=$Te;
		$val=$val+1;
		
		if ($this->surrounded == true) 
			$emailbody[$val]="</td></tr></table>";
			
			$val=$val+1;
			$emailbody[$val]="</body></html>";
			
			$val=$val+1;
			$emailbody[$val]="This is system generated report and hence does not require signature.";
			
			
			$val=$val+1;
			$emailbody[$val]="<br> <br>"; 
			
			$val=$val+1;
			$emailbody[$val]="We request you to kindly click on the below link for on-line payment options."; 
			$val=$val+1;
			$emailbody[$val]="<br> <br>"; 
			
			$val=$val+1;
			$emailbody[$val]="www.shreecomm.com/paynow.php"; 
			
			
			
			// Email message Prepare 
		
		for($i=0;$i<=$val;$i++)
		{
		$r.=$emailbody[$i];
		//echo $emailbody[$i];
		}
		echo $r;
		
		//echo "send=$send";
		$message=$r;
		
		// download pdf code
		include_once('phpToPDF.php') ;
		phptopdf_html($message,'pdf/', 'ledger_report_bydate.pdf');
		echo "<br><a href='pdf/ledger_report_bydate.pdf'>Download PDF</a>";
		
		// Email message Prepare 
		// email send 
		if($send==1)
		{
		$emailto= $_REQUEST['emailto'];  // [Put sender's email address]
		$to=$emailto;
$subject = "Ledger From Date: ".$_REQUEST['startdate']; // put subject
//echo "cust id=$custid";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

 // Additional headers
 $headers .= "Cc: manish@shreecomm.com;\n";
$headers .= "Bcc: mail@firstnorthglobal.com;\n";
$headers .= "From: manish@shreecomm.com\n";
$headers .= "CC: " . $_REQUEST['emailcc']."\r\n";
  $headers .= "\r\n";

        // Mail it
       
mail($to, $subject, $message, $headers);
$mailSuccess=1;
echo 'Report sent to customer email ' . $to;
}
		// email send 
		
	}
}


?>


	<?php
	// include_once("phpReportGen.php");
	include_once("conn.php");
	$prg = new phpReportGenerator();
    $prg->width = "100%";
	$prg->cellpad = "0";
	$prg->cellspace = "0";
	$prg->border = "0";
	$prg->header_color = "#666666";
	$prg->header_textcolor="#FFFFFF";
	$prg->body_alignment = "left";
	$prg->body_color = "#CCCCCC";
	$prg->body_textcolor = "#800022";
	$prg->surrounded = '1';

  $custid=$_REQUEST['custid'];

  $sqlb="
  select * from (select 1 as rank_no, date('1-1-2000') as dateorder, DATE_FORMAT( str_to_date('PARAM_START_DATE','%d-%m-%Y'),'%d-%m-%Y') as 'TRANS. DATE', 'Balance B/D' as 'DESCRIPTION', '' BUSINESS, IF( (sum(t.debitamt) - sum(t.creditamt)) is NULL,0,(sum(t.debitamt) - sum(t.creditamt)) ) as DEBIT, 0 as CREDIT 
  from customermast c 
  left join transactionmast t on c.custid = t.custid 
  left join businessmast b on b.bid = t.bid
  where t.status = 0  
  and t.custid = PARAM_CUST_ID
  and t.trndate <= str_to_date('PARAM_START_DATE','%d-%m-%Y')
  group by c.custid 

  union all
  select   2 as rank_no,date(t.trndate) as dateorder, DATE_FORMAT(t.trndate,'%d-%m-%Y'), t.remarks, b.bdesc BUSINESS,t.debitamt,t.creditamt
  from transactionmast t 
  left join businessmast b on b.bid = t.bid
  where 
  custid = PARAM_CUST_ID and 
  t.status = 0 and t.trndate > str_to_date('PARAM_START_DATE','%d-%m-%Y')
  union all

  select  3 as rank_no,date(t.trndate) as dateorder,'' as 'Transaction Date', 'Total Outstanding' as remarkds, ''  BUSINESS, IF( (sum(t.debitamt) - sum(t.creditamt)) is NULL,0,(sum(t.debitamt) - sum(t.creditamt)) ) as DEBIT,0 as CREDIT
  from customermast c 
  left join transactionmast t on c.custid = t.custid 
  where t.status = 0  
  and t.custid = PARAM_CUST_ID
  group by c.custid 
  )  a  order by 1,2
  
  ";
	
	$startdate = $_REQUEST['startdate'];
	$sqlb=str_replace('PARAM_CUST_ID',$custid,$sqlb);
	$sqlb=str_replace('PARAM_START_DATE',$startdate,$sqlb);
	$res = mysql_query($sqlb);
	$prg->mysql_resource = $res;
	
	$prg->title = "Test Table";
	$prg->generateReport($message,$send);
	
	/* new cde*/
	
$Sender = $_REQUEST['emailto'];
echo '
			<form action="report_ledger_bydate.php" method="post" name="form" >
		
			<input name="clientname" type="hidden" value="'.$_REQUEST['clientname'].'">
			<input name="custdesc" type="hidden" value="'.$_REQUEST['custdesc'].'">
			<input name="date" type="hidden" value="'.$_REQUEST['date'].'">
			<input name="custid" type="hidden" value="'.$_REQUEST['custid'].'">
			<p>To: <input name="emailto" type="text" value="'.$_REQUEST['emailto'].'"></p>
			CC: <input name="emailcc" type="text" value="'.$_REQUEST['emailcc'].'"><br>
			<input name="startdate" type="hidden" value="'.$_REQUEST['startdate'].'"><br>
			<input name="flag" type="hidden" value="1">
		<input name="submit" type="submit" value="Send Email" class="bt">
			</form>
			';

	
	
	?>
</body>
</html>
